package com.xms.webresource.service;

import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * <p>
 * WebResourceContentCoder
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@Service
public class WebResourceContentCoder implements IWebResourceContentCoder{

    @Override
    public String decode(String content) throws UnsupportedEncodingException {
        if (content.isEmpty())
        {
            return content;
        }
        return new String(Base64.getDecoder().decode(content), "UTF-8");
    }

    @Override
    public String encode(String content) throws UnsupportedEncodingException {
        if (content.isEmpty())
        {
            return content;
        }
        return Base64.getEncoder().encodeToString(content.getBytes("UTF-8"));
    }

    @Override
    public String encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    @Override
    public byte[] decodeToByte(String content) {
        if (content.isEmpty())
        {
            return new byte[0];
        }
        return Base64.getDecoder().decode(content);
    }
}
